package content;

import entity.*;
import java.util.*;
import javax.ejb.FinderException;
import javax.naming.*;
import transfer.Order;
import transfer.User;

/**
 *
 * @author Sergey
 */
public class OrderContent {

    public static ArrayList getAllShipping(User courier) {
        try {
            Context context = new InitialContext();
            OrdersLocalHome ordersHome = (OrdersLocalHome) context.lookup("java:comp/env/Orders");
            Collection collection = ordersHome.findAllShipping(courier);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((OrdersLocal) iterator.next()).getOrder());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ArrayList getAllQueued(User warehouse_employee) {
        try {
            Context context = new InitialContext();
            OrdersLocalHome ordersHome = (OrdersLocalHome) context.lookup("java:comp/env/Orders");
            Collection collection = ordersHome.findAllQueued(warehouse_employee);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((OrdersLocal) iterator.next()).getOrder());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ArrayList getAllCommentsForOrder(Order order) {
        try {
            Context context = new InitialContext();
            CommentsLocalHome commentsHome = (CommentsLocalHome) context.lookup("java:comp/env/Comments");
            Collection collection = commentsHome.findAllCommentsForOrder(order);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((CommentsLocal) iterator.next()).getComment());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Order getOrder(Integer id) {
        try {
            Context context = new InitialContext();
            OrdersLocalHome ordersHome = (OrdersLocalHome) context.lookup("java:comp/env/Orders");
            return ((OrdersLocal) ordersHome.findByPrimaryKey(id)).getOrder();
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static ArrayList getAllOrders(User user) {
        try {
            Context context = new InitialContext();
            OrdersLocalHome ordersHome = (OrdersLocalHome) context.lookup("java:comp/env/Orders");
            Collection collection = ordersHome.findAllOrdersForUser(user);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((OrdersLocal) iterator.next()).getOrder());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}